import Blockly from 'blockly/core'
import 'blockly/python_compressed'
import 'blockly/blockly_compressed'
import 'blockly/blocks_compressed'

Blockly.Blocks.function_case = {
  init: function () {
    this.appendValueInput('CASE').setCheck(null).appendField('case :  ')
    this.setInputsInline(true)
    this.setPreviousStatement(true, null)
    this.setNextStatement(true, null)
    this.setColour(230)
    this.setTooltip('')
    this.setHelpUrl('')
  }
}

Blockly.Python.function_case = function (block) {
  const valueName = Blockly.Python.valueToCode(
    block,
    'CASE',
    Blockly.Python.ORDER_NONE
  )
  const code = 'case :' + valueName + '\n'
  return code
}

Blockly.Blocks.function_match = {
  caseCount_: 0,
  init: function () {
    this.appendValueInput('MATCH').setCheck(null).appendField('match  ')
    this.appendDummyInput().appendField(' : ')
    this.appendStatementInput('CASE').setCheck(null)
    this.appendDummyInput().appendField('                }')
    this.setInputsInline(true)
    this.setPreviousStatement(true, null)
    this.setNextStatement(true, null)
    this.setColour(230)
    this.setTooltip('')
    this.setHelpUrl('')
  }
}

Blockly.Python.function_match = function (block) {
  const matchValue = Blockly.Python.valueToCode(
    block,
    'MATCH',
    Blockly.Python.ORDER_NONE
  )
  const caseValue = Blockly.Python.statementToCode(block, 'CASE')
  let code = ''
  code = 'match ' + matchValue + ' : {' + '\n'
  code += caseValue
  code += '}'
  return code
}

Blockly.Blocks.function_match_default = {
  init: function () {
    this.appendValueInput('MATCH').setCheck(null).appendField('match  :')
    this.appendDummyInput().appendField('{')
    this.appendStatementInput('CASE').setCheck(null)
    this.appendValueInput('DEFAULT').setCheck(null).appendField('case   _:')
    this.appendDummyInput().appendField('}')
    this.setInputsInline(true)
    this.setPreviousStatement(true, null)
    this.setNextStatement(true, null)
    this.setColour(230)
    this.setTooltip('')
    this.setHelpUrl('')
  }
}

Blockly.Python.function_match_default = function (block) {
  const matchName = Blockly.Python.valueToCode(
    block,
    'MATCH',
    Blockly.Python.ORDER_ATOMIC
  )
  const caseName = Blockly.Python.statementToCode(block, 'CASE')
  const defaultName = Blockly.Python.valueToCode(
    block,
    'DEFAULT',
    Blockly.Python.ORDER_ATOMIC
  )
  let code = ''
  code = 'match ' + matchName + ' : {' + '\n'
  code += caseName
  code += 'case _:' + defaultName
  code += '}'
  return code
}

export default Blockly
